package com.ititer.dao.impl;

import static com.ititer.util.MyBatisCriteriaFactory.newCriteria;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.ititer.dao.CompetenceDao;
import com.ititer.mapper.CompetenceMapper;
import com.ititer.model.Competence;
import com.ititer.model.CompetenceCriteria;

@Repository
public class CompetenceDaoImpl implements CompetenceDao {
	@Autowired
	private CompetenceMapper competenceMapper;

	@Override
	public Competence getCompetence(Integer accountId, String item) {
		CompetenceCriteria example = newCriteria(CompetenceCriteria.class);
		example.createCriteria().andAccountIdEqualTo(accountId).andItemEqualTo(item);
		List<Competence> competences = competenceMapper.selectByExample(example);
		if (CollectionUtils.isEmpty(competences)) {
			return null;
		}
		return competences.get(0);
	}

	@Override
	public int create(Competence entity) {
		return competenceMapper.insert(entity);
	}

	@Override
	public int update(Competence entity) {
		entity.setLastUpdate(new Date());
		return competenceMapper.updateByPrimaryKey(entity);
	}

	@Override
	public List<Competence> getCompetences(Integer userid) {
		CompetenceCriteria example = newCriteria(CompetenceCriteria.class);
		example.createCriteria().andAccountIdEqualTo(userid);
		List<Competence> competences = competenceMapper.selectByExample(example);
		return competences;
	}
}
